import {tabBar} from '../router/config.js'
const toast =(title)=>{
	uni.showToast({
		title:title,
		icon:'none',
		duration:2000,
		mask:true
	})
}

const dataFormat=(timestamp,type)=>{
    if(!timestamp){
        return '--';
    }
    type=type?type:'Y-m-d'
    let myDate=new Date(timestamp)
    const zero=(value)=> value<10?'0'+value:value;
    let year=myDate.getFullYear();
    let month=zero(myDate.getMonth()+1);
    let day=zero(myDate.getDate());
    let hour=zero(myDate.getHours());
    let minute=zero(myDate.getMinutes());
    let second=zero(myDate.getSeconds());
    return type.replace(/Y|m|d|H|i|s/g,function(matches){
        return{
            Y:year,
            m:month,
            d:day,
            H:hour,
            i:minute,
            s:second
        }[matches]
    })
}

const goto=(path)=>{
    const isTabBar=tabBar.some(item=>path===item)
    if(isTabBar)
    {
        uni.switchTab({url:path})
    }
    return 
}

const goback=()=>{
    uni.navigateBack()
}

export const useCommonFun=()=>{
    return{
        toast,
        dataFormat,
        goto,
        goback
    }
}